package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 通过购物车查看商品
     *
     * @return
     */
    @Override
    public List<ShoppingCart> listAll() {
        ShoppingCart build = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> list = shoppingCartMapper.list(build);
        return list;
    }

    /**
     * 添加一个购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //创建一个对象
        ShoppingCart build = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();
        //先判断是否存在该数据，存在的话加一下num
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(build);

        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            //查出来了，那就做更新
            //取到第一个值
            ShoppingCart shoppingCart = shoppingCarts.get(0);

            build.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(build);

        } else {
            ShoppingCart shoppingCart = new ShoppingCart();
            //没查出来，做插入
            //判断是做菜品插入还是套餐插入
            if (build.getDishId() != null) {
                //插入菜品
                Dish byId = dishMapper.getById(build.getDishId());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());
                shoppingCart.setDishId(build.getDishId());
            }
            if (build.getSetmealId() != null) {
                //插入套餐
                Setmeal byId = setmealMapper.getById(build.getSetmealId());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());
                shoppingCart.setSetmealId(build.getSetmealId());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 更改购物车数量
     *
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //构建对象传参
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        int num = 0;
        ShoppingCart byId = shoppingCartMapper.getById(shoppingCart);
        num = byId.getNumber();
        shoppingCart.setNumber(num - 1);
        //num为0删除菜品
        if(num-1==0){
            shoppingCartMapper.delete(shoppingCart);
        }
        shoppingCartMapper.update(shoppingCart);
    }

    /**
     * 获得目标信息
     *
     * @param id
     */
    @Override
    public ShoppingCart getById(Long id) {
        ShoppingCart shoppingCarts = new ShoppingCart();
        shoppingCarts.setId(id);
        ShoppingCart shoppingCart = shoppingCartMapper.getById(shoppingCarts);
        return shoppingCart;
    }

    /**
     * 清空所有的购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteAll();
    }
}
